home *** CD-ROM | disk | FTP | other *** search
/ Java Certification Exam Guide / McGrawwHill-JavaCertificationExamGuide.iso / pc / Web Links and Code / code / chap27 / answer / Client.java next >
Encoding:
Java Source  |  1997-04-20  |  3.5 KB  |  134 lines

  1. package client;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5. import server.Server;
  6.  
  7. /**
  8.  * Communicates with the database server.
  9.  * @see server.Server
  10.  * @version 1.0
  11.  * @author Barry Boone
  12.  */
  13. public class Client {
  14.  
  15.    /* Data stream to read from server. */
  16.    DataInputStream remoteIn;
  17.  
  18.    /* Data stream to write to server. */
  19.    DataOutputStream remoteOut;
  20.  
  21.    private int port = 5001;
  22.    private Socket sock;
  23.    private String server = null;
  24.       // name of the remote server (null if on same machine)
  25.  
  26.    /**
  27.     * Establish a connection with the database server.
  28.     * @exception IOException  Thrown if there's trouble 
  29.     *   connecting to the server
  30.     */
  31.    void establishConnection() throws IOException {
  32.       try {
  33.          InetAddress serverAddr = InetAddress.getByName(server);
  34.          sock = new Socket(serverAddr.getHostName(), port, true);
  35.  
  36.          remoteIn = new DataInputStream(sock.getInputStream());
  37.          remoteOut = new DataOutputStream(sock.getOutputStream());
  38.  
  39.       } catch (IOException e) {
  40.          System.out.println(e.getMessage() +
  41.             ": Failed to connect to server.");
  42.          throw e;
  43.       }
  44.  
  45.    }
  46.  
  47.    /*
  48.     * Close the connection with the database server.
  49.     */
  50.    void closeConnection() {
  51.       try {
  52.          if (remoteOut != null) {
  53.             remoteOut.close();
  54.             remoteOut = null;
  55.          }
  56.  
  57.          if (remoteIn != null) {
  58.             remoteIn.close();
  59.             remoteIn = null;
  60.          }
  61.  
  62.       } catch (IOException x) {
  63.          System.out.println(x.getMessage());
  64.       } finally {
  65.          try {
  66.             if (sock != null) {
  67.                sock.close();
  68.                sock = null;
  69.             }
  70.          } catch (IOException x) {
  71.             System.out.println(x.getMessage());
  72.          }
  73.       }
  74.    }
  75.  
  76.    int[] getOpenSeats() throws IOException {
  77.       establishConnection();
  78.       remoteOut.writeInt(Server.OP_GET_OPEN_SEATS);
  79.       int numSeats = remoteIn.readInt();
  80.       int[] seats = new int[numSeats];
  81.       for (int i = 0; i < numSeats; i++) 
  82.          seats[i] = remoteIn.readInt();
  83.  
  84.       closeConnection();
  85.       return seats;
  86.    }
  87.  
  88.    String[] getPassengerList() throws IOException {
  89.       establishConnection();
  90.       remoteOut.writeInt(Server.OP_GET_PASSENGER_LIST);
  91.       int numPassengers = remoteIn.readInt();
  92.       String[] passengers = new String[numPassengers];
  93.       for (int i = 0; i < numPassengers; i++) 
  94.          passengers[i] = remoteIn.readUTF();
  95.  
  96.       closeConnection();
  97.       return passengers;
  98.    }
  99.  
  100.    int reservePassenger(String name, int seat) throws IOException{
  101.       establishConnection();
  102.       remoteOut.writeInt(Server.OP_MAKE_RESERVATION);
  103.       remoteOut.writeUTF(name);
  104.       remoteOut.writeInt(seat);
  105.       int rv = remoteIn.readInt();
  106.       closeConnection();
  107.       return rv;
  108.    }
  109.  
  110.    int deletePassenger(String name) throws IOException {
  111.       establishConnection();
  112.       remoteOut.writeInt(Server.OP_DELETE);
  113.       remoteOut.writeUTF(name);
  114.       int rv = remoteIn.readInt();
  115.       closeConnection();
  116.       return rv;
  117.    }
  118.  
  119.    int getSeat(String name) throws IOException {
  120.       establishConnection();
  121.       remoteOut.writeInt(Server.OP_GET_SEAT);
  122.       remoteOut.writeUTF(name);
  123.       int seat = remoteIn.readInt();
  124.       closeConnection();
  125.       return seat;
  126.    }
  127.  
  128.    protected void finalize() throws Throwable {
  129.       super.finalize();
  130.       closeConnection();
  131.    }
  132.  
  133. }
  134.